#include "seq_pairs.h"
#include <iostream>
#include <algorithm>
#include "tab_delimited_str.h"

# define DEBUG_LEVEL 4
#include "debug_func.h"


	using std::string;
using std::istream;
namespace seq_pairs
{
bool skip_header(std::istream& is, std::istringstream& istrm)
{
	debug_func_cerr(5);
	//
	// handle header if present
	//
	string first_line, second_line;

	string col_names	= column_names();
	string underscores	= column_name_underscores();
	
	if (getline(is, first_line)										&&
		first_line.substr(0, col_names.length()) == col_names		&&
		getline(is, second_line)									&&
		second_line.substr(0, underscores.length()) == underscores)
	{	
		// set string stream to bad so that it doesn't get used
		istrm.clear(std::ios::badbit);
		is.putback('\n');
		debug_cerr(5, "Header line");
		return true;
	}
	// go back to beginning
	is.clear();
//	is.seekg(0, std::ios_base::beg);

	// second line might not be read unless first line matches
	// in that case, do not append new line mindlessly
	debug_cerr(5, "First Line = [" << first_line << "]");
	debug_cerr(5, "Second Line = [" << second_line << "]");
	
	if (second_line.length())
		first_line += "\n" + second_line;
	istrm.str(first_line);
	// stream operators swallow initial delimiter, so must add that back in
	is.putback('\n');
	return false;
}

bool skip_header(std::istream& is)
{
	debug_func_cerr(5);
	//
	// handle header if present
	//
	string first_line, second_line;

	string col_names	= column_names();
	string underscores	= column_name_underscores();
	
	if (getline(is, first_line)										&&
		first_line.substr(0, col_names.length()) == col_names		&&
		getline(is, second_line)									&&
		second_line.substr(0, underscores.length()) == underscores)
	{	
		is.putback('\n');
		debug_cerr(5, "Header");
		return true;
	}
	// go back to beginning
	is.clear();
	is.seekg(0, std::ios_base::beg);
	return false;
}

//________________________________________________________________________________________

//		parse_ids_from_line

// 			get first two ids

//________________________________________________________________________________________
/*bool parse_ids_from_line(const string& line, string& id1, string& id2)
{
	string::const_iterator i, end;
	i = line.begin();
	end = line.end();

	// First id delimited by tab: check found and not empty string
	i = std::find(i,  end,  '\t');
	if (i == end || i == line.begin())
		return false;
	id1.assign(line.begin(), i);

	end = std::find(++i,  end,  '\t');

	// Second id delimited by tab or EOS: check not empty string
	if (i == end)
		return false;

	id2.assign(i, end);

	return true;
}
*/

bool parse_ids(std::istream& is, std::string& id1, std::string& id2, 
			   std::string& comment_line, const std::string& comment_letters, 
			   ePARSE_IDS parse_line)
{
	debug_func_cerr(5);
	// read first identifier
	char endc;
	is >> tab_delimited_str('\n', id1, endc);

	// empty line: treat as comment
	if (id1.empty())
	{
        //std::cerr << "????\n";
		// has new line so comment_line can just be printed out as is
		comment_line = "\n";
		return false;
	}

	// is comment
	if (comment_letters.find(id1[0]) != std::string::npos)
	{
		debug_cerr(5, "comment");
		getline(is, comment_line);
		// has new line so comment_line can just be printed out as is
		comment_line = id1 + comment_line + "\n";
		is.putback('\n');
		return false;
	}

	if (endc != '\t')
		throw std::runtime_error("Tab missing");

	is >> tab_delimited_str('\t', id2, endc);

	if (parse_line == ePARSE_WHOLE_LINE)
	{
		is.ignore(32768, '\n');
		is.putback('\n');
		debug_cerr(5, "whole line");
	}
	return true;
}

// get substring which consists of first two ids
bool parse_normalized_ids_sub_string(const string& orig, string& substr, const std::string& comment_letters)
{
	if (comment_letters.find(orig[0]) != std::string::npos)
		return false;

	// return false for empty strings
	if (orig.length() == 0)
		return false;

	string::const_iterator i, end;
	i = orig.begin();
	end = orig.end();
	i = std::find(i,  end,  '\t');
	if (i == end)
		throw std::runtime_error("Invalid data (without tab-separated identifiers)");
	string id1(orig.begin(), i);
	if (id1.empty())
		throw std::runtime_error("Missing first identifier");
	end = std::find(++i,  end,  '\t');
	string id2(i, end);
	if (id2.empty())
		throw std::runtime_error("Missing second identifier");
	if (id1 > id2)
		std::swap(id1, id2);
	substr = id1 + '\t' + id2;
	return true;
}

}



